package com.xiaoxu.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.spzx.manager.mapper.SysRoleMapper;
import com.xiaoxu.spzx.manager.mapper.SysRoleUserMapper;
import com.xiaoxu.spzx.manager.service.SysRoleService;
import com.xiaoxu.spzx.model.dto.system.SysRoleDto;
import com.xiaoxu.spzx.model.entity.system.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;



    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.save(sysRole);
    }

    //分页查询
    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer current, Integer limit) {

        PageHelper.startPage(current,limit);

        //根据条件查询所有的数据
       List<SysRole> list =  sysRoleMapper.findByPage(sysRoleDto);

       //下面是按照要求显示页面数据  ， 比如说前端要求 1页 3条数据, 返回就是1页三条数据
       PageInfo<SysRole> pageInfo  = new PageInfo<>(list);

        return pageInfo;
    }

    //修改角色
    @Override
    public void updateSysRole(SysRole sysRole) {

        sysRoleMapper.update(sysRole);
    }

    //逻辑删除角色
    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.delete(roleId);
    }


    //分配角色 先查询到所有的角色
    @Override
    public Map<String, Object> findAll(Long userId) {

        //1 查所有
        List<SysRole> roleList = sysRoleMapper.findAll();

        //在显示出该用户已经拥有的角色 也就是查看当前用户已经存在的角色

        List<Long>  roleIds =   sysRoleUserMapper.selectRoleIdsByUserId(userId);


        Map<String ,Object> map = new HashMap<>();
        map.put("allRolesList",roleList);
        map.put("sysUserRoles", roleIds);
        return map;
    }
}
